//
//  AppDelegate.m
//  A_program
//
//  Created by BayMax on 15/5/21.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "AppDelegate.h"
#import "RadioViewController.h"
#import "LoginViewController.h"
#import "MusicViewController.h"
#import "NovelViewController.h"
#import "PlayerViewController.h"
#import "Singleton.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface AppDelegate ()

@property (nonatomic, retain) UIButton *playPause;
@property (nonatomic, retain) NSDictionary *dic;

@end

@implementation AppDelegate

- (void)dealloc
{
    [_playPause release];
    [_dic release];
    [_window release];
    [super dealloc];
}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]autorelease];
    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];
    
    
    // 进入动画的图片
    UIImage *image = [UIImage imageNamed:@"title.jpg"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = [[UIScreen mainScreen] bounds];
    [self.window addSubview:imageView];
    [imageView release];
    
    // 进入动画的块
    [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        imageView.alpha = 0;
    } completion:^(BOOL finished) {
        
        // 音乐界面
        MusicViewController *music = [[MusicViewController alloc] init];
        UINavigationController *musicNav = [[UINavigationController alloc] initWithRootViewController:music];
        musicNav.tabBarItem.title = @"音乐";
        [music release];
        
        
        
        // 电台界面
        RadioViewController *radio = [[RadioViewController alloc]init];
        UINavigationController *radioNav = [[UINavigationController alloc]initWithRootViewController:radio];
        radioNav.tabBarItem.title = @"电台";
        [radio release];
        

        // 小说界面
        NovelViewController *novel = [[NovelViewController alloc]init];
        UINavigationController *novelNav = [[UINavigationController alloc]initWithRootViewController:novel];
        novelNav.tabBarItem.title = @"小说";
        [novel release];
        
        
        // 我的界面
        LoginViewController *login = [[LoginViewController alloc] init];
        UINavigationController *loginNav = [[UINavigationController alloc] initWithRootViewController:login];
        loginNav.tabBarItem.title = @"我的";
        [login release];
        
        self.tab = [[UITabBarController alloc] init];
        _tab.viewControllers = @[radioNav, musicNav, novelNav, loginNav];
        [musicNav release];
        [radioNav release];
        [novelNav release];
        [loginNav release];

        [self.window setRootViewController:_tab];
        
        [_tab release];
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        //让view消失
        NSArray *views = [app.tab.view subviews];
        for(id v in views){
            if([v isKindOfClass:[UITabBar class]]){
                [(UITabBar *)v setHidden:YES];
            }
            
        }
        
        self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 49, [UIScreen mainScreen].bounds.size.width, 49)];
        self.myView.backgroundColor = [UIColor whiteColor];
        
        
        // 中间的播放按钮
        self.playPause = [[UIButton alloc] initWithFrame:CGRectMake(self.window.screen.bounds.size.width *5/12 , -20, self.window.screen.bounds.size.width / 6, self.window.screen.bounds.size.width / 6)];
        _playPause.layer.cornerRadius = self.window.screen.bounds.size.width / 6 / 2;
        _playPause.layer.masksToBounds = YES;
        [_playPause setImage:[UIImage imageNamed:@"play1.png"] forState:UIControlStateNormal];
        _playPause.backgroundColor = [UIColor lightGrayColor];
        
        // 按钮图片与上次程序退出时最后的歌曲图片一致
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.window.screen.bounds.size.width / 6, self.window.screen.bounds.size.width / 6)];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        self.dic = [defaults objectForKey:@"dict"];
        [image sd_setImageWithURL:[NSURL URLWithString:[_dic objectForKey:@"coverLarge"]]];
        [_playPause addSubview:image];
        [image release];
        
        [_playPause addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.myView addSubview:_playPause];
        [_playPause release];
        
        UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(10, -11, self.myView.frame.size.width/6, self.myView.frame.size.width/6)];
        UIImage *radioImage = [[UIImage imageNamed:@"novel_123456.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [button1 setImage:radioImage forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(action1:) forControlEvents:UIControlEventTouchUpInside];
        self.lable1 = [[UILabel alloc]initWithFrame:CGRectMake(0, button1.frame.size.height*3/4-1, button1.frame.size.width, button1.frame.size.height/4)];
        self.lable1.font = [UIFont systemFontOfSize:9];
        self.lable1.text = @"电台";
        self.lable1.textAlignment = NSTextAlignmentCenter;
        self.lable1.textColor = [UIColor colorWithWhite:0.3 alpha:1];
        [button1 addSubview:self.lable1];
        [self.myView addSubview:button1];
        
        UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(self.myView.frame.size.width/6+10 , -11, self.myView.frame.size.width/6, self.myView.frame.size.width/6)];
        UIImage *musicImage = [[UIImage imageNamed:@"music_1.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
        [button2 setImage:musicImage forState:UIControlStateNormal];
        [button2 addTarget:self action:@selector(action2:) forControlEvents:UIControlEventTouchUpInside];
        self.lable2 = [[UILabel alloc]initWithFrame:CGRectMake(0, button2.frame.size.height*3/4-1, button2.frame.size.width, button2.frame.size.height/4)];
        self.lable2.font = [UIFont systemFontOfSize:9];
        self.lable2.text = @"音乐";
        self.lable2.textAlignment = NSTextAlignmentCenter;
        self.lable2.textColor = [UIColor colorWithWhite:0.3 alpha:1];
        [button2 addSubview:self.lable2];
        [self.myView addSubview:button2];
        
        UIButton *button3 = [[UIButton alloc]initWithFrame:CGRectMake(self.myView.frame.size.width/6 * 4 - 10, -11, self.myView.frame.size.width/6, self.myView.frame.size.width/6)];
        UIImage *novelImage = [[UIImage imageNamed:@"radio.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [button3 setImage:novelImage forState:UIControlStateNormal];
        [button3 addTarget:self action:@selector(action3:) forControlEvents:UIControlEventTouchUpInside];
        self.lable3 = [[UILabel alloc]initWithFrame:CGRectMake(0, button3.frame.size.height*3/4-1, button3.frame.size.width, button3.frame.size.height/4)];
        self.lable3.font = [UIFont systemFontOfSize:9];
        self.lable3.text = @"有声小说";
        self.lable3.textAlignment = NSTextAlignmentCenter;
        self.lable3.textColor = [UIColor blueColor];
        self.lable3.textColor = [UIColor colorWithWhite:0.3 alpha:1];
        
        [button3 addSubview:self.lable3];
        [self.myView addSubview:button3];
        
        UIButton *button4 = [[UIButton alloc]initWithFrame:CGRectMake(self.myView.frame.size.width*5/6-10, -11, self.myView.frame.size.width/6, self.myView.frame.size.width/6)];
        UIImage *myImage = [[UIImage imageNamed:@"my_123456.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [button4 setImage:myImage forState:UIControlStateNormal];
        [button4 addTarget:self action:@selector(action4:) forControlEvents:UIControlEventTouchUpInside];
        self.lable4 = [[UILabel alloc]initWithFrame:CGRectMake(0, button4.frame.size.height*3/4-1, button4.frame.size.width, button4.frame.size.height/4)];
        self.lable4.font = [UIFont systemFontOfSize:9];
        self.lable4.text = @"我的";
        self.lable4.textAlignment = NSTextAlignmentCenter;
        self.lable4.textColor = [UIColor colorWithWhite:0.3 alpha:1];
        [button4 addSubview:self.lable4];
        [self.myView addSubview:button4];        
        [self.window addSubview:self.myView];
    }];
    
    return YES;
}


- (void)action1:(id)sender{
    
    self.tab.selectedIndex = 0;
    self.lable2.textColor = [UIColor colorWithWhite:0.3 alpha:1];
    self.lable3.textColor = [UIColor colorWithWhite:0.3 alpha:1];
    self.lable4.textColor = [UIColor colorWithWhite:0.3 alpha:1];
    self.lable1.textColor = [UIColor blueColor];
    
    
}
- (void)action2:(id)sender{
    self.tab.selectedIndex = 1;
    self.lable1.textColor = [UIColor colorWithWhite:0.3 alpha:1];
    self.lable3.textColor = [UIColor colorWithWhite:0.3 alpha:1];
    self.lable4.textColor = [UIColor colorWithWhite:0.3 alpha:1];
    self.lable2.textColor = [UIColor blueColor];
    
    
    
    
}
- (void)action3:(id)sender{
    self.tab.selectedIndex = 2;
    self.lable1.textColor = [UIColor colorWithWhite:0.3 alpha:1];
    self.lable2.textColor = [UIColor colorWithWhite:0.3 alpha:1];
    self.lable4.textColor = [UIColor colorWithWhite:0.3 alpha:1];
    self.lable3.textColor = [UIColor blueColor];
    
    
   
    
}
- (void)action4:(id)sender{
    self.tab.selectedIndex = 3;
        self.lable1.textColor = [UIColor colorWithWhite:0.3 alpha:1];
        self.lable2.textColor = [UIColor colorWithWhite:0.3 alpha:1];
        self.lable3.textColor = [UIColor colorWithWhite:0.3 alpha:1];
        self.lable4.textColor = [UIColor blueColor];


    
}



- (void)playAction:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.dic = [defaults objectForKey:@"dict"];
    if (_dic) {
        
        Singleton *sin = [Singleton shareManager];
        
        PlayerViewController *play = [[PlayerViewController alloc] init];
        sin.state = nil;
        play.trackId = [_dic objectForKey:@"trackId"];
        UITabBarController *tab = (UITabBarController *)self.window.rootViewController;
        [tab presentViewController:play animated:YES completion:nil];
 
    }
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [application beginBackgroundTaskWithExpirationHandler:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
